package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kuiandjie.entity.IdeaComment;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class IdeaCommentDao {
	private DBHelp<IdeaComment> db = new DBHelp<IdeaComment>();
	
	private class IdeaCommentMap implements RowMapper<IdeaComment>{

		@Override
		public IdeaComment mapperRow(ResultSet rs) throws SQLException {
			IdeaComment ic = new IdeaComment();
			ic.setContent(rs.getString("content"));
			ic.setEmployeeId(rs.getInt("employeeid"));
			ic.setId(rs.getInt("id"));
			ic.setIdeaId(rs.getInt("ideaid"));
			ic.setName(rs.getString("name"));
			ic.setCreatetime(rs.getString("createtime"));
			return ic;
		}
	}
	
	public List<IdeaComment> findByIdeaId(int id) {
		String sql = "SELECT a.id AS id, content, createtime, employeeid, ideaid, b.name AS name FROM t_ideacomment AS a, t_employee AS b WHERE a.employeeid = b.id AND ideaid = ? ORDER BY createtime DESC";
		return db.executeQueryForList(sql, new IdeaCommentMap(), id);
	}

	public int insertComment(IdeaComment ic) {
		String sql = "INSERT INTO t_ideacomment (content, createtime, employeeid, ideaid) VALUES(?, ?, ?, ?)";
		return db.getAutoKey(sql, ic.getContent(),ic.getCreatetime(), ic.getEmployeeId(), ic.getIdeaId());
	}

	public List<IdeaComment> findByIdAndTime(int ideaId, String lastTime,
			int commentId) {
		String sql = "SELECT a.id AS id, content, createtime, employeeid, ideaid, b.name AS name FROM t_ideacomment AS a, t_employee AS b WHERE a.employeeid = b.id AND a.id <> ? AND ideaid = ? AND createtime >= ? ORDER BY createtime";
		return db.executeQueryForList(sql, new IdeaCommentMap(), commentId, ideaId, lastTime);
	}
}
